package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 分类分页查询
    @GetMapping("/page")
    public Result pageQuery(CategoryPageQueryDTO cpq){
        log.info("分类分页查询,接受到的参数是{}",cpq);
        PageResult categorylist = categoryService.pageQuery(cpq);
        return Result.success(categorylist);
    }

    // 分类新增
    @PostMapping
    public Result insert(@RequestBody CategoryDTO categoryDTO){
        log.info("分类新增,接受到的参数是{}",categoryDTO);
         categoryService.insert(categoryDTO);
        return Result.success();
    }

    // 根据id删除分类
    @DeleteMapping
    public Result deleteById(@RequestParam Integer id){
        log.info("根据id删除分类,接受到的参数为{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }

    // 修改分类
    @PutMapping
    public Result update(@RequestBody Category category){
        log.info("修改分类,收到的参数是{}",category);
        categoryService.update(category);
        return Result.success();
    }

    // 启用禁用分类
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Integer id){
        log.info("启用禁用分类,接受到的参数是{},{}",status,id);
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    // 根据类型查询分类
    @GetMapping("/list")
    public Result selectByType(Integer type){
        log.info("根据类型查询分类,接受到的参数是{}",type);
        List<Category> list =  categoryService.selectByType(type);
        return Result.success(list);
    }
}
